package com.opmsys.controller;

import com.opmsys.pojo.dto.*;
import com.opmsys.pojo.entity.Person;
import com.opmsys.pojo.vo.PersonInfo;
import com.opmsys.result.PageResult;
import com.opmsys.result.Result;
import com.opmsys.service.PersonService;
import com.opmsys.utils.authEnum.StatusEnum;
import com.opmsys.utils.jsonWebToken.JsonWebTokenUtil;
import com.opmsys.utils.redis.RedisUtil;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/person")
public class PersonController {

    private final PersonService personService;
    private final JsonWebTokenUtil jsonWebTokenUtil;
    private final RedisUtil redisUtil;

    public PersonController(PersonService personService, JsonWebTokenUtil jsonWebTokenUtil, RedisUtil redisUtil) {
        this.personService = personService;
        this.jsonWebTokenUtil = jsonWebTokenUtil;
        this.redisUtil = redisUtil;
    }


    /**
     * 用于管理员录入新的人员信息
     * @param person
     * @return
     */
    @PostMapping()
    public Result<Object> add(Person person){

        int repeatedNum = personService.selectPersonByNum(person.getNumber());//重复工号数量
        if (repeatedNum!=0){
            return Result.error("工号不能重复");
        }

        Boolean isAddSuccess  = personService.add(person);
        if (isAddSuccess){
            return Result.successWithMsg("添加用户成功");
        }else {
            return Result.error("添加用户失败！");
        }
    }


    /**
     * 用于管理员更新人员信息
     * @param person
     * @return
     */
    @PutMapping()
    public Result<Object> update(Person person){
        if(person.getStatus() == null){
            return Result.error("缺失信息");
        }
        //确保管理员正常ban人
        if (person.getStatus().equals(StatusEnum.BAN.status)){
            redisUtil.delete(person.getNumber().toString());
        }

        if (personService.change(person)){
            return Result.successWithMsg("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }


    /**
     * 模糊查询（用户名或工号）并分页
     * @param pagingSearchAboutPerson
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> search(PagingSearchAboutPerson pagingSearchAboutPerson){
        List<PersonInfo> list = personService.pagingSelectPersonListByFuzzyKeyWord(pagingSearchAboutPerson);
        long total = personService.pageTotal(pagingSearchAboutPerson);
        PageResult pageResult = new PageResult(total,list);
        return Result.successWithMsgAndData("查询成功",pageResult);
    }


    /**
     * 用户修改密码
     * @param passwordAbout
     * @return
     */
    @PutMapping(value = "/password")
    public Result<Object> changePasswordByUser(PasswordAbout passwordAbout){
        if (personService.changePasswordByUser(passwordAbout)){
            return Result.successWithMsg("修改成功");
        }else {
            return Result.error("修改失败");
        }
    }

//    //管理员修改用户密码
//    @PostMapping(value = "/admin/change-password")
//    public Result<Object> changePasswordByAdmin(@RequestBody PasswordAbout passwordAbout){
//        if (personService.changePasswordByAdmin(passwordAbout)){
//            return Result.successWithMsg("修改成功");
//        }else {
//            return Result.error("修改失败");
//        }
//    }



//    //模糊查询
//    @GetMapping(value = "/fuzzy-search")
//    public Result<Object> fuzzySearch(@RequestBody FuzzySearchAbout fuzzySearchAbout){
//        return Result.successWithMsgAndData("查询成功",personService.selectPersonListByFuzzyKeyWord(fuzzySearchAbout));
//    }




}
